package com.ztjy.purchase.controller;

import com.github.pagehelper.PageInfo;
import com.ztjy.annonation.MyLog;
import com.ztjy.bean.CBrand;
import com.ztjy.bean.CProduct;
import com.ztjy.bean.CType;
import com.ztjy.purchase.biz.CBrandBiz;
import com.ztjy.purchase.biz.CProductBiz;
import com.ztjy.purchase.biz.CTypeBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/purchase/brand")
@SessionAttributes({"date2","brandid"})
public class CBrandController {
    @Autowired
    private CBrandBiz cbbiz;
    @Autowired
    private CTypeBiz ctbiz;
    @Autowired
    private CProductBiz cpbiz;

    //每次执行前会优先 将mid放入request范围
    @ModelAttribute
    public void initModuleData(HttpSession session) {
        session.removeAttribute("mid");
        session.setAttribute("mid", 17);
    }

    @MyLog(value = "查询所有品牌操作")
    @RequestMapping("/brandAll")
    public String brandList(Model model, CBrand cBrand, @RequestParam(value = "pageno",defaultValue = "1") Integer pageNo){
        Integer pageSize=6;
        PageInfo<CBrand> cbpage = cbbiz.findAllCBrand(cBrand, pageNo, pageSize);
        Date date = new Date();
        model.addAttribute("date2",date);
        model.addAttribute("cbpage",cbpage);
        model.addAttribute("cBrand",cBrand);
        return "forward:/purchase/brand/brandList.html";
    }

    @MyLog(value = "跳入品牌修改")
    @RequestMapping("/goBrandUpdate/{bid}")
    public String goUpdate(@PathVariable("bid") Integer bid,Model model){
        CBrand cBrandByBid = cbbiz.findCBrandByBid(bid);
        model.addAttribute("upbrand",cBrandByBid);
        return "forward:/purchase/brand/brandUpdate.html";
    }

    @MyLog(value = "品牌修改操作")
    @RequestMapping("/doBrandUpdate")
    public String goBrandUpdate(Model model,CBrand cBrand){
        boolean flag = cbbiz.updateCBrand(cBrand);
        if(flag){
            return "redirect:/purchase/brand/brandAll";
        }else {
            return "forward:/purchase/brand/brandUpdate.html";
        }

    }

    @MyLog(value = "品牌添加操作")
    @RequestMapping("/addBrand")
    public String addBrand(Model model,CBrand cBrand){
        boolean flag = cbbiz.addCBrand(cBrand);
        if(flag){
            return "redirect:/purchase/brand/brandAll";
        }else {
            return "redirect:/purchase/brand/brandAdd.html";
        }
    }

    /**
     * 品牌状态修改
     * @param bid
     * @return
     */
    @MyLog(value = "品牌注销/恢复操作")
    @RequestMapping("/updateState/{bid}")
    public String updateState(@PathVariable("bid") Integer bid){
        CBrand cBrand = cbbiz.findCBrandByBid(bid);
        if(cBrand.getBrandStatus()==1){
            cBrand.setBrandStatus(2);
            List<CType> cTypesByBId = ctbiz.findCTypesByBId(bid);
            for (CType t:cTypesByBId){
                t.setTypeStatus(2);
                ctbiz.updateCType(t);
                List<CProduct> typeId = cpbiz.findbyTypeId(t.getTypeId());
                for (CProduct p:typeId){
                    p.setProductStatus(2);
                    cpbiz.updateProduct(p);
                }
            }
        }
        cbbiz.updateCBrand(cBrand);
        return "redirect:/purchase/brand/brandAll";
    }
}
